package com.chengma.service.impl;


import com.chengma.domain.TbQuestion;
import com.chengma.domain.TbUserCollectedQuestion;
import com.chengma.domain.TbUserVisitedQuestion;
import com.chengma.mapper.*;
import com.chengma.service.ITbQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 问题Service业务层处理
 *
 * @author tck
 * @date 2025-03-19
 */
@Service
public class TbQuestionServiceImpl implements ITbQuestionService {
    @Autowired
    private TbQuestionMapper tbQuestionMapper;

    @Autowired
    private TbUserCollectedQuestionMapper tbUserCollectedQuestionMapper;

    @Autowired
    private TbCommentMapper tbCommentMapper;
    @Autowired
    private TbUserVisitedQuestionMapper tbUserVisitedQuestionMapper;

    /**
     * 查询问题
     *
     * @param id 问题主键
     * @return 问题
     */
    @Override
    public TbQuestion selectTbQuestionById(String id) {
        return tbQuestionMapper.selectTbQuestionById(id);
    }

    /**
     * 查询问题列表
     *
     * @param tbQuestion 问题
     * @return 问题
     */
    @Override
    public List<TbQuestion> selectTbQuestionList(TbQuestion tbQuestion) {
        List<TbQuestion> tbQuestions = tbQuestionMapper.selectTbQuestionList(tbQuestion);
        for (TbQuestion tbq : tbQuestions) {
            String questionId = tbq.getId();//问题id
            String createUser = tbq.getCreateUser();//问题人
            //用于判断是否收藏
            TbUserCollectedQuestion tbUserCollectedQuestion = new TbUserCollectedQuestion();
            tbUserCollectedQuestion.setQuestionId(questionId);
            tbUserCollectedQuestion.setUsername(createUser);
            tbq.setIs_collected(tbUserCollectedQuestionMapper.selectTbUserCollectedQuestionList(tbUserCollectedQuestion).size() > 0);
            //用于汇总问题浏览数量
            tbq.setVisitedNum(tbUserVisitedQuestionMapper.getCommentVisitedQuestion(Integer.valueOf(questionId)));
            //用于评论问题浏览数量
            tbq.setCommentNum(tbCommentMapper.getCommentLikes(Integer.valueOf(questionId)));
        }
        return tbQuestions;
    }

    /**
     * 新增问题
     *
     * @param tbQuestion 问题
     * @return 结果
     */
    @Override
    public int insertTbQuestion(TbQuestion tbQuestion) {
        tbQuestion.setCreateTime(new Date());
        return tbQuestionMapper.insertTbQuestion(tbQuestion);
    }

    /**
     * 修改问题
     *
     * @param tbQuestion 问题
     * @return 结果
     */
    @Override
    public int updateTbQuestion(TbQuestion tbQuestion) {
        tbQuestion.setUpdateTime(new Date());
        return tbQuestionMapper.updateTbQuestion(tbQuestion);
    }

    /**
     * 批量删除问题
     *
     * @param ids 需要删除的问题主键
     * @return 结果
     */
    @Override
    public int deleteTbQuestionByIds(String[] ids) {
        return tbQuestionMapper.deleteTbQuestionByIds(ids);
    }

    /**
     * 删除问题信息
     *
     * @param id 问题主键
     * @return 结果
     */
    @Override
    public int deleteTbQuestionById(String id) {
        return tbQuestionMapper.deleteTbQuestionById(id);
    }
}
